/*
 * GranDTWindowsView.java
 */

package grandtwindows;

import grandtwindows.internas.JInternalFrameAltaParticipante;
import java.beans.PropertyVetoException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * The application's main frame.
 */
public class GranDTWindowsView extends FrameView {

    public GranDTWindowsView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = null;//resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = null;//resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = GranDTWindowsApp.getApplication().getMainFrame();
            aboutBox = new GranDTWindowsAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        GranDTWindowsApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        desktopPaneMain = new javax.swing.JDesktopPane();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        menuItemLogin = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JSeparator();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        menuTools = new javax.swing.JMenu();
        menuItemAltaParticipante = new javax.swing.JMenuItem();
        menuItemArmarEquipo = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        menuItemAltaTorneo = new javax.swing.JMenuItem();
        menuItemInscribirUsuarioTorneo = new javax.swing.JMenuItem();
        menuItemAceptarUsuarioTorneo = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        menuItemRegistrarPuntajes = new javax.swing.JMenuItem();
        menuItemGenerarTablasPosiciones = new javax.swing.JMenuItem();
        menuItemMostrarEquipos = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        menuItemImportarJugadores = new javax.swing.JMenuItem();
        menuItemImportarEquipos = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        fileChooser = new javax.swing.JFileChooser();

        mainPanel.setName("mainPanel"); // NOI18N

        desktopPaneMain.setName("desktopPaneMain"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPaneMain, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPaneMain, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(grandtwindows.GranDTWindowsApp.class).getContext().getResourceMap(GranDTWindowsView.class);
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        menuItemLogin.setText(resourceMap.getString("menuItemLogin.text")); // NOI18N
        menuItemLogin.setName("menuItemLogin"); // NOI18N
        menuItemLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemLoginActionPerformed(evt);
            }
        });
        fileMenu.add(menuItemLogin);

        jSeparator4.setName("jSeparator4"); // NOI18N
        fileMenu.add(jSeparator4);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(grandtwindows.GranDTWindowsApp.class).getContext().getActionMap(GranDTWindowsView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        menuTools.setText(resourceMap.getString("menuTools.text")); // NOI18N
        menuTools.setName("menuTools"); // NOI18N

        menuItemAltaParticipante.setText(resourceMap.getString("menuItemAltaParticipante.text")); // NOI18N
        menuItemAltaParticipante.setToolTipText(resourceMap.getString("menuItemAltaParticipante.toolTipText")); // NOI18N
        menuItemAltaParticipante.setName("menuItemAltaParticipante"); // NOI18N
        menuItemAltaParticipante.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemAltaParticipanteActionPerformed(evt);
            }
        });
        menuTools.add(menuItemAltaParticipante);

        menuItemArmarEquipo.setText(resourceMap.getString("menuItemArmarEquipo.text")); // NOI18N
        menuItemArmarEquipo.setToolTipText(resourceMap.getString("menuItemArmarEquipo.toolTipText")); // NOI18N
        menuItemArmarEquipo.setName("menuItemArmarEquipo"); // NOI18N
        menuItemArmarEquipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemArmarEquipoActionPerformed(evt);
            }
        });
        menuTools.add(menuItemArmarEquipo);

        jSeparator1.setName("jSeparator1"); // NOI18N
        menuTools.add(jSeparator1);

        menuItemAltaTorneo.setText(resourceMap.getString("menuItemAltaTorneo.text")); // NOI18N
        menuItemAltaTorneo.setToolTipText(resourceMap.getString("menuItemAltaTorneo.toolTipText")); // NOI18N
        menuItemAltaTorneo.setName("menuItemAltaTorneo"); // NOI18N
        menuItemAltaTorneo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemAltaTorneoActionPerformed(evt);
            }
        });
        menuTools.add(menuItemAltaTorneo);

        menuItemInscribirUsuarioTorneo.setText(resourceMap.getString("menuItemInscribirUsuarioTorneo.text")); // NOI18N
        menuItemInscribirUsuarioTorneo.setToolTipText(resourceMap.getString("menuItemInscribirUsuarioTorneo.toolTipText")); // NOI18N
        menuItemInscribirUsuarioTorneo.setName("menuItemInscribirUsuarioTorneo"); // NOI18N
        menuItemInscribirUsuarioTorneo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemInscribirUsuarioTorneoActionPerformed(evt);
            }
        });
        menuTools.add(menuItemInscribirUsuarioTorneo);

        menuItemAceptarUsuarioTorneo.setText(resourceMap.getString("menuItemAceptarUsuarioTorneo.text")); // NOI18N
        menuItemAceptarUsuarioTorneo.setToolTipText(resourceMap.getString("menuItemAceptarUsuarioTorneo.toolTipText")); // NOI18N
        menuItemAceptarUsuarioTorneo.setName("menuItemAceptarUsuarioTorneo"); // NOI18N
        menuItemAceptarUsuarioTorneo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemAceptarUsuarioTorneoActionPerformed(evt);
            }
        });
        menuTools.add(menuItemAceptarUsuarioTorneo);

        jSeparator2.setName("jSeparator2"); // NOI18N
        menuTools.add(jSeparator2);

        menuItemRegistrarPuntajes.setText(resourceMap.getString("menuItemRegistrarPuntajes.text")); // NOI18N
        menuItemRegistrarPuntajes.setToolTipText(resourceMap.getString("menuItemRegistrarPuntajes.toolTipText")); // NOI18N
        menuItemRegistrarPuntajes.setName("menuItemRegistrarPuntajes"); // NOI18N
        menuItemRegistrarPuntajes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemRegistrarPuntajesActionPerformed(evt);
            }
        });
        menuTools.add(menuItemRegistrarPuntajes);

        menuItemGenerarTablasPosiciones.setText(resourceMap.getString("menuItemGenerarTablasPosiciones.text")); // NOI18N
        menuItemGenerarTablasPosiciones.setToolTipText(resourceMap.getString("menuItemGenerarTablasPosiciones.toolTipText")); // NOI18N
        menuItemGenerarTablasPosiciones.setName("menuItemGenerarTablasPosiciones"); // NOI18N
        menuItemGenerarTablasPosiciones.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemGenerarTablasPosicionesActionPerformed(evt);
            }
        });
        menuTools.add(menuItemGenerarTablasPosiciones);

        menuItemMostrarEquipos.setText(resourceMap.getString("menuItemMostrarEquipos.text")); // NOI18N
        menuItemMostrarEquipos.setToolTipText(resourceMap.getString("menuItemMostrarEquipos.toolTipText")); // NOI18N
        menuItemMostrarEquipos.setName("menuItemMostrarEquipos"); // NOI18N
        menuItemMostrarEquipos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemMostrarEquiposActionPerformed(evt);
            }
        });
        menuTools.add(menuItemMostrarEquipos);

        jSeparator3.setName("jSeparator3"); // NOI18N
        menuTools.add(jSeparator3);

        menuItemImportarJugadores.setText(resourceMap.getString("menuItemImportarJugadores.text")); // NOI18N
        menuItemImportarJugadores.setToolTipText(resourceMap.getString("menuItemImportarJugadores.toolTipText")); // NOI18N
        menuItemImportarJugadores.setName("menuItemImportarJugadores"); // NOI18N
        menuItemImportarJugadores.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemImportarJugadoresActionPerformed(evt);
            }
        });
        menuTools.add(menuItemImportarJugadores);

        menuItemImportarEquipos.setText(resourceMap.getString("menuItemImportarEquipos.text")); // NOI18N
        menuItemImportarEquipos.setToolTipText(resourceMap.getString("menuItemImportarEquipos.toolTipText")); // NOI18N
        menuItemImportarEquipos.setName("menuItemImportarEquipos"); // NOI18N
        menuItemImportarEquipos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemImportarEquiposActionPerformed(evt);
            }
        });
        menuTools.add(menuItemImportarEquipos);

        menuBar.add(menuTools);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 230, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        fileChooser.setName("fileChooser"); // NOI18N

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void menuItemAltaParticipanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemAltaParticipanteActionPerformed
        try {
            JInternalFrameAltaParticipante internal = new JInternalFrameAltaParticipante();
            desktopPaneMain.add(internal);

            internal.setSelected(true);
            internal.setVisible(true);
        } catch (PropertyVetoException ex) {
            Logger.getLogger(GranDTWindowsView.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_menuItemAltaParticipanteActionPerformed

    private void menuItemArmarEquipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemArmarEquipoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuItemArmarEquipoActionPerformed

    private void menuItemAltaTorneoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemAltaTorneoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuItemAltaTorneoActionPerformed

    private void menuItemInscribirUsuarioTorneoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemInscribirUsuarioTorneoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuItemInscribirUsuarioTorneoActionPerformed

    private void menuItemAceptarUsuarioTorneoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemAceptarUsuarioTorneoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuItemAceptarUsuarioTorneoActionPerformed

    private void menuItemRegistrarPuntajesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemRegistrarPuntajesActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuItemRegistrarPuntajesActionPerformed

    private void menuItemGenerarTablasPosicionesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemGenerarTablasPosicionesActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuItemGenerarTablasPosicionesActionPerformed

    private void menuItemMostrarEquiposActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemMostrarEquiposActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuItemMostrarEquiposActionPerformed

    private void menuItemImportarJugadoresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemImportarJugadoresActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setAcceptAllFileFilterUsed(false);
        fileChooser.setFileFilter(new FileNameExtensionFilter("CSV document (*.csv)", "csv"));
        if(fileChooser.showOpenDialog(this.getFrame())==JFileChooser.APPROVE_OPTION){
            fileChooser.getSelectedFile().getPath();
        }

    }//GEN-LAST:event_menuItemImportarJugadoresActionPerformed

    private void menuItemImportarEquiposActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemImportarEquiposActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setAcceptAllFileFilterUsed(false);
        fileChooser.setFileFilter(new FileNameExtensionFilter("CSV document (*.csv)", "csv"));
        if(fileChooser.showOpenDialog(this.getFrame())==JFileChooser.APPROVE_OPTION){
            fileChooser.getSelectedFile().getPath();
        }
    }//GEN-LAST:event_menuItemImportarEquiposActionPerformed

    private void menuItemLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemLoginActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuItemLoginActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane desktopPaneMain;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem menuItemAceptarUsuarioTorneo;
    private javax.swing.JMenuItem menuItemAltaParticipante;
    private javax.swing.JMenuItem menuItemAltaTorneo;
    private javax.swing.JMenuItem menuItemArmarEquipo;
    private javax.swing.JMenuItem menuItemGenerarTablasPosiciones;
    private javax.swing.JMenuItem menuItemImportarEquipos;
    private javax.swing.JMenuItem menuItemImportarJugadores;
    private javax.swing.JMenuItem menuItemInscribirUsuarioTorneo;
    private javax.swing.JMenuItem menuItemLogin;
    private javax.swing.JMenuItem menuItemMostrarEquipos;
    private javax.swing.JMenuItem menuItemRegistrarPuntajes;
    private javax.swing.JMenu menuTools;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}
